package com.hc.stepKeydbservice;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hc.logdbservice.LogDatabaseHelper;
import com.hc.model.StepKey;
import com.hc.model.TestLogModel;
/**
 * 
 * 训练保存数据库操作业务类
 * @author Administrator
 *
 */
public class KeyManager {

	private 	KeyDatabaseHelper helper;
	private SQLiteDatabase db;
	public KeyManager(Context context) {
		helper = new KeyDatabaseHelper(context);
		db = helper.getWritableDatabase();
	}
/**
 * 
 * 添加训练记录
 * @param key
 */
	public void addTestKey(StepKey key){
		
		
		 db.beginTransaction();	//开始事务
	        try {
	        		db.execSQL("INSERT INTO stepKey VALUES(?, ?, ?, ?, ?)", new Object[]{key.getId(),
	        				key.getAddTime(), key.getKey(), key.getDrillLength(),key.getName()});
	        		db.setTransactionSuccessful();	//设置事务成功完成
	        } finally {
	        	db.endTransaction();	//结束事务
	        }
	}
/**
 * 
 * 删除训练记录
 * @param id
 */
	public void delTestKey(String id){
		
		
		 db.beginTransaction();	//开始事务
	        try {
	        		db.execSQL("delete  from stepKey where id = ?", new Object[]{id});
	        		db.setTransactionSuccessful();	//设置事务成功完成
	        } finally {
	        	db.endTransaction();	//结束事务
	        }
	}
	/**
	 * 
	 * 返回保存的所有训练记录
	 * @return
	 */
   public List<StepKey> getStepKeyModels(){
	   ArrayList<StepKey> keys = new ArrayList<StepKey>();
		Cursor c = queryTheCursor();
       while (c.moveToNext()) {
    	 StepKey key = new StepKey();
    	 key.setId(c.getString(c.getColumnIndex("id")));
    	 key.setAddTime(c.getString(c.getColumnIndex("addTime")));
    	 key.setName(c.getString(c.getColumnIndex("name")));
    	 key.setKey(c.getString(c.getColumnIndex("key")));
    	 key.setDrillLength(c.getString(c.getColumnIndex("drillLength")));
    	 keys.add(key);
       }
       c.close();
       return keys;
	   

   } 
   
	/**
	 * query all log, return cursor
	 * @return	Cursor
	 */
	public Cursor queryTheCursor() {
       Cursor c = db.rawQuery("SELECT * FROM stepKey order by Id desc", null);
       return c;
	}
	
	/**
	 * close database
	 */
	public void closeDB() {
		db.close();
	}

}
